<template>
	<view class="tenant-pages">
		<SelectHouse :info="selectInfo" page='tenantInfo'></SelectHouse>
		<view class="title-info">
			<view class="line"></view>
			<view class="title">承租人信息</view>
		</view>
		<view class="form-block">
			<view class="form-item">
				<view class="form-name">照片</view>
				<view class="form-info">
					<image :src="form.faceUrl || imgStorage+'/login/face.png'" class="photo" />
				</view>
			</view>
			<view class="form-item">
				<view class="form-name">承租人</view>
				<view class="form-info">
					<view class="form-content">{{form.tenantName}}</view>
					<view class="form-text green" v-if="form.faceAuthenticationTime">已实名</view>
					<view class="form-text red" v-else>未实名</view>
				</view>
			</view>
			<view class="form-item">
				<view class="form-name">身份证号码</view>
				<view class="form-info">
					<view class="form-content">{{form.tenantIdCard || '-'}}</view>
				</view>
			</view>
			<view class="form-item">
				<view class="form-name">联系方式</view>
				<view class="form-info">
					<view class="form-content">{{form.tenantPhone || '-'}}</view>
				</view>
			</view>
		</view>
		<view class="title-info">
			<view class="line"></view>
			<view class="title">家庭成员</view>
		</view>
		<view v-if="list.length>0">
			<view class="info-item" v-for="item in list" :key="item.id">
				<view class="title-item">
					<view class="name-item">
						<view class="name">{{item.name || '-'}}</view>
						<view class="type">{{item.relationship || '-'}}</view>
					</view>
					<view class="status normal" v-if="item.isContact == 1">紧急联系人</view>
				</view>
				<view class="tenant-info">
					<view class="tenant-title">联系电话</view>
					<view class="tenant-item">
						<view class="tenant-text">{{desensitizeAnyPhone(item.contactNumber) || '-'}}</view>
						<view class="iconfont icon-a-boda2" v-if="item.contactNumber" @click="call(item.contactNumber)">
						</view>
					</view>
				</view>
				<view class="tenant-info">
					<view class="tenant-title">工作单位</view>
					<view class="tenant-item">
						<view class="tenant-text">{{item.company || '-'}}</view>
					</view>
				</view>
			</view>
		</view>
		<view v-else>
			<view class="info-item">
				<image :src="imgStorage+'/login/nodata.png'" class="image" />
				<view class="noData">暂无数据</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		SelectHouse
	} from '@/components/SelectHouse.vue'
	import {
		getStorage
	} from '@/util/auth.js';
	import {
		getTenantInformation
	} from '@/api/authentication.js'
	export default {
		components: {
			SelectHouse
		},
		data() {
			return {
				form: {
					faceUrl: '',
					tenantName: '',
					faceAuthenticationTime: '',
					tenantIdCard: '',
					tenantPhone: ''
				},
				list: [],
				selectInfo: {}
			}
		},
		methods: {
			obtainTenantInformation() {
				getTenantInformation().then(res => {
					const safeRes = Array.isArray(res) ? res : [];
					const targetId = this.selectInfo?.id;

					if (targetId) {
						const matchedData = safeRes.find(item => item.id === targetId);
						this.form.faceUrl = matchedData.faceUrl;
						this.form.tenantName = matchedData.tenantName;
						this.form.faceAuthenticationTime = matchedData.faceAuthenticationTime
						this.form.tenantIdCard = matchedData.tenantIdCard;
						this.form.tenantPhone = matchedData.tenantPhone;
						this.list = matchedData.familyMembers || []
					} else {
						console.error("selectInfo或ID缺失");
					}
				})
			},
			desensitizeAnyPhone(phone) {
				if (/^1[3-9]\d{9}$/.test(phone)) { // 手机号
					return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
				} else if (/^0\d{2,3}-\d{7,8}(-\d+)?$/.test(phone)) { // 座机
					return phone.replace(/(\d{3,4}-)\d{4}(\d{4})/, '$1****$2');
				}
				return phone; // 非标准格式原样返回
			}
		},
		onLoad() {
			this.obtainTenantInformation()
		},
		onShow() {
			this.selectInfo = getStorage('selectInfo')
			this.obtainTenantInformation()
		}
	}
</script>

<style lang="scss" scoped>
	.tenant-pages {
		padding: 30rpx;



		.title-info {
			display: flex;
			margin-top: 20rpx;
			align-items: center;

			.line {
				width: 16rpx;
				height: 36rpx;
				background-color: $theme-color;
				border-radius: 30rpx;
				margin-right: 14rpx;
			}

			.title {
				font-size: 32rpx;
				font-weight: bold;
			}
		}

		.form-block {
			margin: 30rpx 0;
			background: #fff;
			padding: 30rpx;
			border-radius: 30rpx;

			.form-item:last-child {
				margin-bottom: 0;
				padding-bottom: 0;
				border-bottom: none;
			}

			.form-item {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 40rpx;
				border-bottom: 1px solid #f7f7f7;
				padding-bottom: 40rpx;

				.form-name {
					font-size: 30rpx;
				}

				.form-info {
					display: flex;
					align-items: center;

					.photo {
						width: 100rpx;
						height: 100rpx;
						background-color: #f5f5f5;
						border-radius: 50%;
					}

					.form-content {
						font-size: 30rpx;
					}

					.form-text {
						margin-left: 20rpx;
						background-color: $success-background;
						color: $success-color;
						height: 54rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						font-size: 28rpx;
						width: 120rpx;
						border-radius: 10rpx;
					}

					.green {
						background-color: $success-background;
						color: $success-color;
					}

					.red {
						background-color: #fff2f0;
						color: #ff4d4f;
					}
				}
			}
		}

		.info-item {
			background-color: #fff;
			padding: 30rpx;
			margin-top: 20rpx;
			border-radius: 20rpx;

			.image {
				display: flex;
				width: 190rpx;
				height: 140rpx;
				margin: auto;
			}

			.noData {
				display: flex;
				font-size: 25rpx;
				color: #767676;
				margin-top: 20rpx;
				justify-content: center;
				align-items: center;
			}

			.title-item {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.name-item {
					display: flex;
					align-items: center;

					.name {
						font-weight: bold;
						margin-right: 20rpx;
						font-size: 32rpx;
					}

					.type {
						font-size: 26rpx;
						width: 80rpx;
						height: 40rpx;
						background-color: #eeeeee;
						color: #aaaaaa;
						display: flex;
						align-items: center;
						justify-content: center;
						border-radius: 10rpx;
					}
				}

				.status {
					font-size: 26rpx;
					width: 150rpx;
					height: 40rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					border-radius: 10rpx;
				}
			}

			.tenant-info {
				display: flex;
				margin-top: 30rpx;

				.tenant-title {
					font-size: 30rpx;
					margin-right: 20rpx;
				}

				.tenant-item {
					display: flex;
					align-items: center;

					.iconfont {
						margin-left: 16rpx;
						color: $theme-color;
					}
				}
			}
		}
	}
</style>